package com.tanhua.backstage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.backstage.service.MessageListService;
import com.tanhua.backstage.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("manage/messages")
public class CommentController {

    @Autowired
    private MessageListService messageListService;

    /**
     * 控制层实现功能:根据消息ID获取所有的评论列表信息
     * @param pageSize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param messageId
     * @return
     */
    @RequestMapping("comments")
    public ResponseEntity<PageResult> getCommentLists(
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam("sortProp") String sortProp,
            @RequestParam("sortOrder") String sortOrder,
            @RequestParam("messageID") String messageId) {
        try {
            PageResult pageResult = messageListService.getCommentLists(page, pageSize, sortProp, sortOrder, messageId);
            //根据返回结果,进行判断,返回前端需要的值
            if (ObjectUtil.isEmpty(pageResult)) {
                return ResponseEntity.ok(null);
            }
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
    }


}
